// 云函数入口文件
const cloud = require('wx-server-sdk')
const cheerio  = require('cheerio');
const axios = require('axios');


cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  let url = event.url;
  console.log('CSDN 解析')
  return new Promise(function(resolve,reject){
    axios.get(url)
    .then(function (response) {
     // console.log(response);
      let htmldata = response.data;
     // console.log(htmldata)
      $ = cheerio.load(htmldata);
      let title = $('#articleContentId').html()
      console.log(title)
      let avatar = $('img.avatar_pic').attr('src')
      console.log(avatar)
      let author = $('a.follow-nickName').html()
      console.log(author)
      let time = $('span.time').html()
      console.log(time)
      let type = $('a.tag-link').html()
      console.log(type)
      let reshtml = $('#article_content').html()
      resolve({
        title,
        htmldata:reshtml,
        articledata:{
          avatar,
          author,
          time,
          type
        }
      })
    })
    .catch(function (error) {
      reject(error)
    });
  })
  
}